package com.jiecao.news.jiecaonews.util;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.jiecao.news.jiecaonews.util.constants.Constants;

public class AlarmUtil {
	/**
	 * 启动定时
	 * 
	 * @param delayTime
	 * @param cls
	 */
	public static  void startAlarm( Class<?> cls,Context act) {
		AlarmManager manager = (AlarmManager) act.getSystemService(act.ALARM_SERVICE);
		Intent intent = new Intent(act, cls);
		PendingIntent pendingIntent = PendingIntent.getService(
				act, Constants.PULL_SERVICE_REQUESTCODE, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		// FIXME
		long  triggerTime = System.currentTimeMillis();
		manager.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime,
				Constants.PULL_SERVER_REPEAT_TIME, pendingIntent);
	}

	/**
	 * 停止定时
	 * 
	 * @param cls
	 */
	public static  void stopAlarm(Class<?> cls,Context context) {
		AlarmManager manager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
		Intent intent = new Intent(context, cls);
		PendingIntent pendingIntent = PendingIntent.getService(
				context, Constants.PULL_SERVICE_REQUESTCODE, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		manager.cancel(pendingIntent);
	}
}
